<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>怡臣 | 店铺管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="/Public/admin/adminlte/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/Public/admin/adminlte/bootstrap/css/bootstrap-treeview.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/font-awesome/font-awesome.min.css">
    <!-- Ionicons -->
    <!--link rel="stylesheet" href="/Public/admin/adminlte/plugins/ionicons/ionicons.min.css"-->
    <!-- Theme style -->
    <link rel="stylesheet" href="/Public/admin/adminlte/dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="/Public/admin/adminlte/dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/iCheck/flat/blue.css">
    <!-- Morris chart -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/morris/morris.css">
    <!-- jvectormap -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/jvectormap/jquery-jvectormap-1.2.2.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/daterangepicker/daterangepicker.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="/Public/admin/adminlte/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="/Public/admin/adminlte/plugins/html5shiv/html5shiv.min.js"></script>
    <script src="/Public/admin/adminlte/plugins/html5shiv/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="row">
    <div class="col-md-12">
        <div class="box">
            <form action="<?php echo U('Freight/saveAddress',array('id'=>$id), '');?>" method="post" onsubmit="return checkforms()">
                <input type="hidden" name="id" value="<?php echo ($id); ?>">
                <input type="hidden" name="index" value="<?php echo ($index); ?>">
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <tbody>
                        <tr>
                            <th class=""><input type="checkbox" class="grid-select-all"></th>
                            <th class="col-md-2">省</th>
                            <th class="col-md-2">市</th>
                            <th class="col-md-8">区</th>
                        </tr>

                        <?php if(is_array($list)): $i = 0; $__LIST__ = $list;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?><tr class="lv1" data-id="<?php echo ($vo["region_id"]); ?>">
                                <td><input type="checkbox" class="grid-item" data-id="<?php echo ($vo["region_id"]); ?>" name="first[]" value="<?php echo ($vo["region_id"]); ?>" <?php if($vo['is_show'] == true): ?>checked<?php endif; ?> ></td>
                                <td><span class="name-editable editable-click-1" data-id="<?php echo ($vo["region_id"]); ?>"
                                          data-type="text" data-pk="1"><?php echo ($vo["local_name"]); ?></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
                                        href="javascript:;" class="label label-success open1">+</a></td>
                                <td></td>
                                <td></td>
                            </tr><?php endforeach; endif; else: echo "" ;endif; ?>
                        </tbody>
                    </table>

                </div>
                <div class="box-footer clearfix">
                    <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;

                    <input type="submit" class="btn btn-sm btn-primary grid-refresh" value="保存">

                </div>
            </form>
        </div>
    </div>
</div>

</body>
<!-- 底部 -->
<script src="/Public/admin/adminlte/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="/Public/admin/adminlte/bootstrap/js/bootstrap.min.js"></script>

<!-- AdminLTE App -->
<script src="/Public/admin/adminlte/dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="/Public/admin/adminlte/dist/js/demo.js"></script>
<script src="/Public/admin/adminlte/plugins/iCheck/icheck.min.js"></script>
<script>
    function checkforms() {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        var first = [];
        var twice = [];
        var second = [];
        $('input[name="first[]"]').each(function () {
            if (this.checked) {
                first.push($(this).val());
            }
        });
        $('input[name="twice[]"]').each(function () {
            if (this.checked) {
                twice.push($(this).val());
            }
        });
        $('input[name="second[]"]').each(function () {
            if (this.checked) {
                second.push($(this).val());
            }
        });
        var id = $('input[name=id]').val();
        $.ajax({
            type: "post",
            url: "<?php echo U('Freight/saveAddress','');?>",
            data: {id:id,first:first,twice:twice,second:second},
            success: function (msg) {
                if(msg.status == false) {
                    alert(msg.message);
                    return false;
                } else {
                    parent.layer.close(index);
                }
            },
            error: function (msg) {
                alert(msg.message);
                return false;
            }
        });

        return false;
    }
</script>
<script data-exec-on-popstate>

    $(function () {
        $('tbody').on('click','.second',function(){
            _this=$(this);
            var lv2c=false;
            $(this).parent().find('.second').each(function(){
                if($(this)[0].checked){
                    _this.parents('.lv2').find('.twice').prop('checked',true);
                    var pid=_this.parents('.lv2').find('.twice').next('span').attr('parent-id');
                    _this.parents('.lv2').siblings('.lv1[data-id='+pid+']').find('input').prop('checked',true);
                    lv2c=true;
                    return;
                }

                /*var pid=_this.parents('.lv2').find('.twice').next('span').attr('parent-id');
                _this.parents('.lv2').siblings('.lv1[data-id='+pid+']').find('input').prop('checked',false);*/
            });
            if(!lv2c){
                _this.parents('.lv2').find('.twice').prop('checked',false);
            }
            var lv1c=false;
            var p_id=$(this).parents('.lv2').find('.twice').next().attr('parent-id');
            $('input[parent-id='+p_id+']').each(function () {
                if($(this)[0].checked){
                    _this.parents('.lv2').siblings('.lv1[data-id='+p_id+']').find('input').prop('checked',true);
                    lv1c=true;
                    return;
                }
            })
            if(!lv1c){
                _this.parents('.lv2').siblings('.lv1[data-id='+p_id+']').find('input').prop('checked',false);
            }
        })
        //所有区域全选
        $('.grid-select-all').change(function () {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });
        //省选中 下级全选
        $('.grid-item').change(function () {
            var childs = $(this).parents('tr').nextUntil('.lv1');
            if (this.checked) {
                childs.each(function () {
                    $(this).find('.grid-item').prop("checked", true);
                })
            } else {
                childs.each(function () {
                    $(this).find('.grid-item').prop("checked", false);
                })
            }
        });
        //市选中 下级全选
        $('tbody').on('change', '.twice', function () {
            var lv1c=false;
            _this=$(this);
            var p_id=$(this).attr('parent-id');
            $('input[parent-id='+p_id+']').each(function () {
                if($(this)[0].checked){
                    _this.parents('.lv2').siblings('.lv1[data-id='+p_id+']').find('input').prop('checked',true);
                    lv1c=true;
                    return;
                }
            })
            if(!lv1c){
                _this.parents('.lv2').siblings('.lv1[data-id='+p_id+']').find('input').prop('checked',false);
            }

            $(this).each(function () {
                if (this.checked) {
                    $(this).parents('.lv2').find('.second').prop("checked", true);
                    //var pid=$(this).parents('.lv2').find('.twice').next('span').attr('parent-id');
                    //$(this).parents('.lv2').siblings('.lv1[data-id='+pid+']').find('input').prop('checked',true);
                } else {
                    $(this).parents('.lv2').find('.second').prop("checked", false);
                    //var pid=$(this).parents('.lv2').find('.twice').next('span').attr('parent-id');
                    //$(this).parents('.lv2').siblings('.lv1[data-id='+pid+']').find('input').prop('checked',false);
                }
            });
        });

        $('.open1').on('click', function (e) {
            //隐藏/展开
            $(this).each(function () {
                var text = $(this).text();
                if(text == '+'){
                    $(this).text('-');
                    $(this).parents('tr').nextUntil($(".lv1")).show();
                } else {
                    $(this).text('+');
                    $(this).parents('tr').nextUntil($(".lv1")).hide();
                }
            });

            var id = $(this).prev('span').attr('data-id');
            var _this = $(this);
            var html = '';
            /**
             <tr>
             <td><input type="checkbox" class="grid-item" data-id="<?php echo ($vo["region_id"]); ?>"></td>
             <td><span class="name-editable editable-click-1" data-id="<?php echo ($vo["region_id"]); ?>" data-type="text" data-pk="1"><?php echo ($vo["local_name"]); ?></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
             href="javascript:;" class="label label-success open1">+</a></td>
             <td><span class="name-editable editable-click-2" data-id="<?php echo ($vo["region_id"]); ?>" data-type="text" data-pk="2"></span></td>
             <td><span class="name-editable editable-click-3" data-id="<?php echo ($vo["region_id"]); ?>" data-type="text" data-pk="3"></span></td>
             </tr>
             */
            //首次加载
            var next_data_id = _this.parents('tr').next('tr').find('.grid-item').hasClass('twice');
            if (!id || next_data_id == true) {
                return false;
            }
            var index_id = $('input[name=id]').val();
            $.ajax({
                type: "get",
                url: "<?php echo U('Freight/getChildsAddress','');?>",
                data: {id: id, index_id:index_id},
                success: function (msg) {

                    var status = false;
                    if (msg.data['length'] > 0) {
                        for (var i = 0; i < msg.data['length']; i++) {
                            html += '<tr class="lv2">';
                            html += '<td></td>';
                            html += '<td><span class="name-editable editable-click-1" data-id="" data-type="text" data-pk="1"></span></td>';
                            html += '<td><input type="checkbox" class="grid-item twice" data-id="' + msg.data[i]['cid'] + '" name="twice[]" value="' + msg.data[i]['cid'] + '" parent-id="' + msg.data[i]['cpid'] + '"';
                            if (msg.data[i]['is_show']) {
                                html += ' checked ';
                                status = true;
                            }
                            html += '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="name-editable editable-click-2" parent-id="' + msg.data[i]['cpid'] + '" data-type="text" data-pk="2">' + msg.data[i]['cname'] + '</span></td>';
                            html += '<td>';
                            var _childs = msg.data[i]['childs'];
                            if (_childs.length > 0 && _childs[0]['vid'] != null) {
                                for (var j=0; j < _childs.length; j++) {
                                    html += '<input type="checkbox" class="grid-item second" data-id="' + _childs[j]['vid'] + '" name="second[]" value="' + _childs[j]['vid'] + '"';
                                    if (_childs[j]['is_show']) {
                                        html += ' checked ';
                                        status = true;
                                    }
                                    html += '>';
                                    html += '<span class="name-editable editable-click-3" parent-id="' + _childs[j]['vpid'] + '" data-type="text" data-pk="3">' + _childs[j]['vname'] + '</span>&nbsp;&nbsp;';
                                }
                            }
                            html += '</td></tr>';
                        }
                    }
                    _this.parent().parent().after(html);
                    if (!status) {
                        var checked=_this.parents('.lv1').find('input[type="checkbox"]')[0].checked;
                         if(checked){
                         _this.parents('.lv1').nextUntil($(".lv1")).find('input').prop("checked", true);
                         }else{
                         _this.parents('.lv1').nextUntil($(".lv1")).find('input').prop("checked", false);
                         }
                    }

                },
                error: function (msg) {
                    alert(msg.message);
                }
            });
        });


        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        


        $('.batch-choose').on('click', function (e) {

            var data = [];
            var exists = parent.$('.ui-sortable').find('li input');
            var exists_ids = [];
            exists.each(function () {
                exists_ids.push($(this).val());
            });

            var qyname = $('#qyname').val();
            var firstunit = $('#firstunit').val();
            var firstprice = $('#firstprice').val();
            var continueunit = $('#continueunit').val();
            var continueprice = $('#continueprice').val();
            var values = qyname + '-' + firstunit + '-' + firstprice + '-' + continueunit + '-' + continueprice;

            var html = '';
            html += '<li class="">';
            html += '<input type="hidden" name="value[]" value="' + values + '">';
            html += '<span class="handle ui-sortable-handle">';
            html += '<i class="fa fa-ellipsis-v"></i>\n\r';
            html += '<i class="fa fa-ellipsis-v"></i>';
            html += '</span>\n\r';
            html += '<small class="label label-warning"> 区域名称 : ' + qyname + '</small>\n\r';
            html += '<small class="label label-warning"> 首重 : ' + firstunit + 'Kg</small>\n\r';
            html += '<small class="label label-success"> 首重运费 : ' + firstprice + '元</small>';
            html += '<small class="label label-success"> 续重 : ' + continueunit + 'Kg</small>';
            html += '<small class="label label-success"> 续重费 : ' + continueprice + '元</small>';
            html += '<div class="label"><a href="javascript:void(0)" class="add_area">添加地区</a></div>';
            html += '<div class="tools">';
            html += '<i class="fa fa-trash-o icondel"></i>';
            html += '</div>';
            html += '</li>';

            parent.$('.ui-sortable').append(html);
            parent.$('.col-sm-offset-2').removeClass('hide');

            parent.layer.close(index);
        })

    });
</script>
</html>